package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerTraceHistoryQuery;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customerTraceHistory")
public class CustomerTraceHistoryController {

    @Autowired
    private ICustomerTraceHistoryService customerTraceHistoryServiceImpl;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerTraceHistoryQuery qo) {

        //根据 字典目录sn communicationMethod 查询出对应的字典明细
        List<SystemDictionaryItem> communicationMethods = systemDictionaryItemService.selectDicItemTitleByDicSn("communicationMethod");
        model.addAttribute("communicationMethods", communicationMethods);

        PageInfo<CustomerTraceHistory> pageInfo = customerTraceHistoryServiceImpl.query(qo);
        model.addAttribute("result", pageInfo);
        return "/customerTraceHistory/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        customerTraceHistoryServiceImpl.delete(id);
        return new JsonResult("删除成功", true);

    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CustomerTraceHistory customerTraceHistory, Long id) {
        if (id != null) {
            customerTraceHistoryServiceImpl.update(customerTraceHistory);
        } else {
            customerTraceHistoryServiceImpl.save(customerTraceHistory);
        }
        return new JsonResult("新增/编辑成功", true);
    }
}
